package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.service.integrityregulation.IntegrityRegulationService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by zouxun on 2018/12/13
 */
@Component("I1029")
@Scope("prototype")
public class I1029 extends BaseComponent {
    private static Logger logger = LoggerFactory.getLogger(I1029.class);

    @Autowired
    private IntegrityRegulationService integrityRegulationService;
    @Autowired
    private HandingData handingData;

    @Override
    public List<ComponentResult> handlerM() {
        logger.info("调用I1029组件,处理M表.................");
        String catalogId= "M-1";
        List<ComponentResult> componentResultList=ruleCheck(catalogId);
        return componentResultList;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        logger.info("调用I1029组件,处理BM表.................");
        String catalogId= "BM-1";
        List<ComponentResult> componentResultList=ruleCheck(catalogId);
        return componentResultList;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        logger.info("调用I1029组件,处理DM表.................");
        String catalogId= "DM-1";
        List<ComponentResult> componentResultList=ruleCheck(catalogId);
        return componentResultList;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        logger.info("调用I1029组件,处理BDM表.................");
        String catalogId= "BDM-1";
        List<ComponentResult> componentResultList=ruleCheck(catalogId);
        return componentResultList;
    }

    private List<ComponentResult> ruleCheck(String catalogId){
        List<ComponentResult> componentResults = new ArrayList<>();

        Map<String,String> checkItemMap = new HashMap<>();

        checkItemMap.put("FXR", "发行人");
        checkItemMap.put("ZWRZGJ", "债务融资工具");
        checkItemMap.put("MJSMS", "募集说明书");
        checkItemMap.put("ZCXS", "主承销商");
        checkItemMap.put("CXT", "承销团");
        checkItemMap.put("CXXY", "承销协议");
        checkItemMap.put("BJJD", "簿记建档");
        checkItemMap.put("BJGLR", "簿记管理人");
        checkItemMap.put("JYSXH", "交易商协会");
        checkItemMap.put("SQS", "上清所");
        checkItemMap.put("BJS", "北金所");
        checkItemMap.put("SNJYQ", "三年及一期");
        checkItemMap.put("GZR", "工作日");
        checkItemMap.put("JJR", "节假日");
        checkItemMap.put("FJMZH", "非居民账户");

        DocumentModel documentModel = super.getExtractData();

        try{
            ChapterModel chapterModel = documentModel.getChapterModel(catalogId);

            if(chapterModel!=null){
                Set<String> checkItemSet = checkItemMap.keySet();

                for(String checkItem:checkItemSet){
                    ComponentResult componentResult = handingData.getComponentResult(catalogId,catalogId,getRuleTitle(),"重要释义-(熊猫债)".concat(checkItemMap.get(checkItem)),"正确",false,getRegulationId(),chapterModel.getStartPage(),null,"01");

                    KVText kvTextKey = getKVTextKey(catalogId,"SY",checkItem);

                    List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();

                    if(kvTextKey!=null){
                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();

                        TextLayout textLayout = kvTextKey.getTextLayout();
                        if(textLayout!=null){
                            textPosition.setContent(textLayout.getText());
                            textPosition.setX(textLayout.getLeft());
                            textPosition.setY(textLayout.getTop());
                            componentResult.setPageNum(textLayout.getPageNumber());
                            textPositionList.add(textPosition);

                            if(checkItem.equals("GZR")){
                                String text = getValueText(catalogId,checkItem);

                                if(StringUtils.isNotEmpty(text)){
                                    if(text.contains("中国") || text.contains("国内") || text.contains("大陆") || text.contains("中华人民共和国")){
                                        componentResult.setPromptMessage("正确");
                                    }else{
                                        componentResult.setPromptMessage("该文档重要释义不完备");
                                        componentResult.setAnnotationFlag(true);
                                        componentResult.setExist("02");
                                    }
                                }else{
                                    componentResult.setPromptMessage("该文档重要释义不完备");
                                    componentResult.setAnnotationFlag(true);
                                    componentResult.setExist("02");
                                }
                            }

                            if(checkItem.equals("CXT")){
                                String text = getKeyText(catalogId,checkItem);

                                if(StringUtils.isNotEmpty(text)){
                                    if(text.equals("承销团协议")){
                                        componentResult.setPromptMessage("该文档重要释义不完备");
                                        componentResult.setAnnotationFlag(true);
                                        componentResult.setExist("02");

                                        textPositionList.clear();
                                    }
                                }
                            }

                        }else{
                            componentResult.setPromptMessage("该文档重要释义不完备");
                            componentResult.setAnnotationFlag(true);
                            componentResult.setExist("02");
                        }
                    }else{
                        componentResult.setPromptMessage("该文档重要释义不完备");
                        componentResult.setAnnotationFlag(true);
                        componentResult.setExist("02");
                    }

                    componentResult.setTextPosition(textPositionList);
                    componentResults.add(componentResult);
                }

            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return componentResults;
    }


    private String getValueText(String catalogId,String item){
        KVText kvTextValue = getKVTextValue(catalogId,"SY",item);

        if(kvTextValue!=null){
            String text = kvTextValue.getText();
            if(StringUtils.isNotEmpty(text)){
                return text;
            }
        }

        return null;
    }

    private String getKeyText(String catalogId,String item){
        KVText kvTextKey = getKVTextKey(catalogId,"SY",item);

        if(kvTextKey!=null){
            String text = kvTextKey.getText();
            if(StringUtils.isNotEmpty(text)){
                return text;
            }
        }

        return null;
    }
}
